package com.shuqi.model.parse.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.BooksInfo;
import com.shuqi.model.bean.RecommendInfo;
import com.shuqi.model.bean.TopicInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 推荐解析器
 * 
 * @author zhuchen
 * 
 */
public class RecommendParser extends HandlerBase {

    private int inWhich;
    private final int BOOKNAME = 1;
    private final int AUTHOR = 2;
    private final int BOOKTYPE = 3;
    private final int BOOKDESCRIPTION = 4;

    private RecommendInfo recommendInfo;
    private TopicInfo topicInfo;
    private BooksInfo booksInfo;
    private BookInfo bookInfo;

    @Override
    public Object getParsedData() {

        return recommendInfo;
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        recommendInfo = new RecommendInfo();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if (localName.equals("Topic")) {
            topicInfo = new TopicInfo();
            topicInfo.setImgType(isNull(attributes, "jumpType"));
            topicInfo.setTopicId(isNull(attributes, "topicId"));
            topicInfo.setTopicName(isNull(attributes, "topicName"));
            topicInfo.setImgUrl(isNull(attributes, "imgUrl"));
            topicInfo.setUrl(isNull(attributes, "url"));
            recommendInfo.getTopicInfoList().add(topicInfo);
        } else if (localName.equals("Books")) {
            booksInfo = new BooksInfo();
            booksInfo.setColumnId(isNull(attributes, "columnId"));
            booksInfo.setTitle(isNull(attributes, "title"));
            booksInfo.setPageCount(isNull(attributes, "pageCount"));
            booksInfo.setTotalCount(isNull(attributes, "totalCount"));
            booksInfo.setColor(isNull(attributes, "color"));
            recommendInfo.getBooksList().add(booksInfo);

        } else if (localName.equals("Book")) {
            bookInfo = new BookInfo();
            bookInfo.setBookId(isNull(attributes, "bookId"));
            bookInfo.setBookCoverImgUrl(isNull(attributes, "imgUrl"));
            bookInfo.setBookHideState(isNull(attributes, "hide"));
            String coverState = isNull(attributes, "cr_cover_isopen");
            if (coverState != null) {
                try {
                    bookInfo.setCoverHideState(Integer.valueOf(coverState));
                } catch (Exception e) {

                }
            }
            bookInfo.setDiscount("");
            booksInfo.getBookInfo().add(bookInfo);
        } else if (localName.equals("BookName")) {
            this.inWhich = BOOKNAME;
        } else if (localName.equals("Author")) {
            this.inWhich = AUTHOR;
        } else if (localName.equals("BookType")) {
            this.inWhich = BOOKTYPE;
        } else if (localName.equals("Description")) {
            this.inWhich = BOOKDESCRIPTION;
        } else {
            this.inWhich = 0;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            switch (this.inWhich) {
                case BOOKNAME:
                    this.bookInfo.setBookName((bookInfo.getBookName() == null ? "" : bookInfo
                            .getBookName()) + value);
                    break;
                case AUTHOR:
                    this.bookInfo.setBookAuthorName((bookInfo.getBookAuthorName() == null ? ""
                            : bookInfo.getBookAuthorName()) + value);
                    break;
                case BOOKTYPE:
                    this.bookInfo.setBookType((bookInfo.getBookType() == null ? "" : bookInfo
                            .getBookType()) + value);
                    break;
                case BOOKDESCRIPTION:
                    this.bookInfo.setBookIntro((bookInfo.getBookIntro() == null ? "" : bookInfo
                            .getBookIntro()) + value);
                    break;
            }
        }
        super.characters(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }
}
